﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.Destex.Satis
{
    public partial class YeniOdeme : XForm
    {
        int MusteriId;

        public YeniOdeme(int MusteriId)
        {
            InitializeComponent();
            this.MusteriId = MusteriId;
        }

        decimal artibakiye = 0;
        private void YeniOdeme_Load(object sender, System.EventArgs e)
        {
            this.Text += "- " + AppSession.Lookups.DX_Musteri.FindByDX_MusteriId(MusteriId).Name;

            gleCurrency.Properties.Assign(RI(Rp.Currency));
            gleCurrency.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

            gridView1.Init("DX_GetOdemeYap", MusteriId);
            gridView1.AddColumn("YeniOdenenTl", typeof(decimal));

            gridView1.MakeColumnInvisible("DX_MusteriId");
            gridView1.FormatAsNumber(1, "ToplamMetraj", "TutarTl", "OdenenTl", "BakiyeTl", "YeniOdenenTl");
            gridView1.AddSummary("TutarTl", "OdenenTl", "BakiyeTl", "YeniOdenenTl");
            gridView1.MakeReadOnly();
            dateEdit1.Properties.DisplayFormat.FormatString = "D";

            chkUseBalance.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            if (gridView1.Dataset().Tables[1].Rows.Count > 0)
            {
                artibakiye = gridView1.Dataset().Tables[1].Rows[0]["AlacakTl"].ToDecimal();
                if (artibakiye > 0)
                {
                    chkUseBalance.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    chkUseBalance.Caption = string.Format("{0:#,###.##}", artibakiye) + " TL.'lik müşteri bakiyesini kullan";
                }
            }
            chkUseBalance.CheckedChanged += ChkUseBalance_CheckedChanged;

            gleCurrency.EditValue = 4;
            dateEdit1.DateTimeChanged += dateEdit1_DateTimeChanged;
            dateEdit1.DateTime = DateTime.Today;

            seTutar.EditValueChanged += seTutar_EditValueChanged;
            gleCurrency.EditValueChanged += gleCurrency_EditValueChanged;
            gridView1.BestFitColumns();
        }

        void gleCurrency_EditValueChanged(object sender, EventArgs e)
        {
            RfrshTutar();
        }

        void seTutar_EditValueChanged(object sender, EventArgs e)
        {
            RfrshTutar();
        }

        decimal TutarTl = 0;
        void RfrshTutar()
        {
            decimal tutar = seTutar.EditValue.ToDecimal();
            string Currency = gleCurrency.Text;
            TutarTl = tutar * dictCur[Currency];

            if (TutarTl > 0||chkUseBalance.Checked)
                gridView1.MakeReadOnly("YeniOdenenTl");
            else
                gridView1.MakeReadOnly();

            lblConversion.Text = string.Format("{0:#,##0.##} TL. (" + Currency + "/TL = {1:0.000})", TutarTl, dictCur[Currency]);
        }


        Dictionary<string, decimal> dictCur = new Dictionary<string, decimal>();
        void dateEdit1_DateTimeChanged(object sender, System.EventArgs e)
        {
            dictCur.Clear();
            dictCur["TL "] = 1;
            dictCur["CHF"] = 0;
            DataTable dtCur = ExecuteDataTable("DX_GetCurrency", dateEdit1.DateTime);
            foreach (DataRow r in dtCur.Rows)
                dictCur[r["CurrencyCode"].ToStr()] = r["Selling"].ToDecimal();
            RfrshTutar();
        }

        private void ChkUseBalance_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            panelControl1.Visible = !chkUseBalance.Checked;
            RfrshTutar();
        }

        private void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();

            if (chkUseBalance.Checked)
                TutarTl = artibakiye;

            decimal Fark = TutarTl - gridView1.DataTable().Compute("SUM(YeniOdenenTl)", string.Empty).ToDecimal();

            if (chkUseBalance.Checked)
                if (Fark > 0)
                {
                    TutarTl -= Fark;
                    Fark = 0;
                }

            if (Fark > 1)
            {
                if (XtraMessageBox.Show("Ödemenin " + string.Format("{0:#,###.##}", Fark) + " TL.lik kısmının müşteri bakiyesi olarak kaydedilmesini onaylıyor musunuz?", "Fazla Ödeme", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                    return;
            }
            else if (Fark < -1)
            {
                XtraMessageBox.Show("Satışlar için " + string.Format("{0:#,###.##}", -Fark) + " TL.lik fazla ödeme girdiniz. Toplamı baştaki TL. rakamla eşitlemeden kaydedemezsiniz.");
                return;
            }

            ExecuteNonQuery("DX_InsOdeme", MusteriId, gleCurrency.EditValue.ToInt(), seTutar.EditValue.ToDecimal(), TutarTl, Fark,
                dateEdit1.DateTime, gridView1.SerializeChanges(), chkUseBalance.Checked, WhoAmI);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}